package subraSoft.centaurus.model.util;

import java.io.File;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

public final class CenConfigurationUtil
{
	private static final String TEMP_SUBFOLDER_NAME = "temp.subfolder.name";

	public static final String HIBERNATE_DIALECT = "hibernate.dialect";
	public static final String BASE_DRIVER = "jdbc.driverClassName";
	public static final String BASE_URL = "jdbc.url";
	public static final String BASE_USER = "jdbc.username";
	public static final String BASE_PASS = "jdbc.password";
	public static final String PROYECT_NAME = "project.artifactId";
	public static final String PROYECT_VERSION = "project.version";


	private static final String CONF_FILE = "conf.properties";
	private static CenConfigurationUtil instance;
	private Properties configurationProperties;
	private static Logger logger = Logger.getLogger(CenConfigurationUtil.class);

	private String tempFolder;

	private CenConfigurationUtil()
	{
		// CARGA DE PROPERTIES
		configurationProperties = new Properties();
		InputStream is = CenConfigurationUtil.class.getClassLoader().getResourceAsStream(CONF_FILE);
		try {
			configurationProperties.load(is);
		} catch (Exception e) {
			System.err.println("No se encuentra el archivo de configuracion "+ CONF_FILE);
			e.printStackTrace();
			System.exit(-1);
		}

		// CREACION DE CARPETA DE TRABAJO DEL PROGRAMA
		tempFolder = System.getProperty("user.home")+File.separator+getProperty(TEMP_SUBFOLDER_NAME);
		File temp = new File(tempFolder);
		if( !temp.exists() )
			temp.mkdirs();
	}

	public static CenConfigurationUtil getInstance()
	{
		if(instance == null)
			instance = new CenConfigurationUtil();
		return instance;
	}

	public String getProperty(String key)
	{
		String prop = configurationProperties.getProperty(key);
		if( prop == null )
		{
			logger.error("La propieded " + key + " no esta definida en " + CONF_FILE);
		}
		return prop;
	}

	public int getIntProperty(String key)
	{
		int prop = -1;
		String sProp = configurationProperties.getProperty(key);
		if( sProp == null )
		{
			logger.error("La propieded " + key + " no esta definida en " + CONF_FILE);
		}
		else
		{
			try
			{
				prop = Integer.parseInt(sProp);
			}
			catch(Exception e)
			{
				logger.error("La propiedad " + key + " no es un numero entero");
			}
		}
		return prop;
	}

	public String getTempFolder() {
		return tempFolder;
	}

}
